//
//  LGReportDetailPhotoController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/2.
//  患者、医生自己上传的病历

#import "LGReportDetailPhotoController.h"

#define viewHeight  (Screen_H-topBarHeight-viewPix(120))

@interface LGReportDetailPhotoController ()<UICollectionViewDelegate,UICollectionViewDataSource>

/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UILabel *numberLabel;
/**   */
@property (nonatomic , strong)NSArray *photoArray;

@end

@implementation LGReportDetailPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.collectionView];
    [self.view addSubview:self.numberLabel];
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.photoArray = dataDic[@"image_files"];
    self.numberLabel.text = [NSString stringWithFormat:@"1/%ld",self.photoArray.count];
    [self.collectionView reloadData];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UIImageView *photoView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), viewHeight)];
    photoView.contentMode = UIViewContentModeScaleAspectFit;
    [photoView sd_setImageWithURL:[NSURL URLWithString:LGNSString(self.photoArray[indexPath.item][@"image_url"])]];
    [cell.contentView addSubview:photoView];
    return cell;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger index = self.collectionView.contentOffset.x/(Screen_W-viewPix(32));
    self.numberLabel.text = [NSString stringWithFormat:@"%ld/%ld",index+1,self.photoArray.count];
}

-(UICollectionView *)collectionView{
    if(!_collectionView){
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.itemSize = CGSizeMake(Screen_W-viewPix(32), viewHeight);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(viewPix(16), 0, Screen_W-viewPix(32), viewHeight) collectionViewLayout:layout];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.pagingEnabled = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(UILabel *)numberLabel{
    if(!_numberLabel){
        _numberLabel = [UILabel lableWithText:@"" colorString:@"#FFFFFF" textFont:LGFontWeight(13, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _numberLabel.frame = CGRectMake((Screen_W-viewPix(64))/2.0, viewHeight-viewPix(40)-bottomSafeBarHeight/2.0, viewPix(64), viewPix(24));
        _numberLabel.backgroundColor = [UIColor colorWithString:@"#808080"];
        _numberLabel.cornerRidus = viewPix(12);
    }
    return _numberLabel;
}

@end




